package com.whq.thrift.api.web.admin.service.consumelog.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whq.thrift.api.common.spring.datasource.DataSourceContextHolder;
import com.whq.thrift.api.web.admin.db.consumelog.ConsumeLog;
import com.whq.thrift.api.web.admin.db.consumelog.ConsumeLogMapper;
import com.whq.thrift.api.web.admin.db.methodinfo.MethodInfoMapper;
import com.whq.thrift.api.web.admin.service.consumelog.ConsumeLogService;
import com.whq.thrift.api.web.admin.util.DateFormatUtil;

@Service
public class ConsumeLogServiceImpl implements ConsumeLogService {

	@Autowired
	ConsumeLogMapper consumeLogMapper;

	@Autowired
	MethodInfoMapper methodInfoMapper;

	public void addRecord(ConsumeLog record) {
		DataSourceContextHolder.set("dataSourceConsumeLog");
		if (record == null) {
			return;
		}
		Date now = new Date();
		record.setCreatetTime(now);
	}

	public List<ConsumeLog> findRecordsByMap(Map<String, Object> whereParams, int beginIndex, int maxCount) {
		DataSourceContextHolder.set("dataSourceConsumeLog");
		if (whereParams == null) {
			whereParams = new HashMap<String, Object>(0);
		}
		whereParams.put("beginIndex", beginIndex);
		whereParams.put("maxCount", maxCount);
		return this.consumeLogMapper.findRecordsByMap(whereParams);
	}

	public int findRecordCount(Map<String, Object> whereParams) {
		DataSourceContextHolder.set("dataSourceConsumeLog");
		return this.consumeLogMapper.findRecordsCount(whereParams);
	}

	public ConsumeLog findRecordById(String id, String time) {
		DataSourceContextHolder.set("dataSourceConsumeLog");
		ConsumeLog consumeLog = new ConsumeLog();
		consumeLog.setTime(DateFormatUtil.formatStringToFullDate(time));
		int tableSeq = consumeLog.getTableSeq();
		consumeLog = null;
		return this.consumeLogMapper.getRecordByKey(tableSeq, id);
	}

	public List<ConsumeLog> statByApiCode(Map<String, Object> whereParams) {
		DataSourceContextHolder.set("dataSourceConsumeLog");
		if (whereParams == null) {
			whereParams = new HashMap<String, Object>(0);
		}
		return this.consumeLogMapper.statByApiCode(whereParams);
	}

	public List<ConsumeLog> statByCode(Map<String, Object> whereParams) {
		DataSourceContextHolder.set("dataSourceConsumeLog");
		if (whereParams == null) {
			whereParams = new HashMap<String, Object>(0);
		}
		return this.consumeLogMapper.statByCode(whereParams);
	}

	public List<ConsumeLog> queryIceCostTimeCountData(Map<String, Object> whereParams) {
		DataSourceContextHolder.set("dataSourceConsumeLog");
		if (whereParams == null) {
			whereParams = new HashMap<String, Object>(0);
		}
		return this.consumeLogMapper.queryIceCostTimeCountData(whereParams);
	}

	public void replaceStatDateApiData(int tableSeq, String statDay, String startTime, String endTime) {
		DataSourceContextHolder.set("dataSourceConsumeLog");
		this.consumeLogMapper.replaceStatDateApiData(tableSeq, statDay, startTime, endTime);
	}

	public void replaceStatHourApiData(int tableSeq, String statDay, String startTime, String endTime, int statHour) {
		DataSourceContextHolder.set("dataSourceConsumeLog");
		this.consumeLogMapper.replaceStatHourApiData(tableSeq, statDay, startTime, endTime, statHour);
	}

	public void delDataApiData(String thresholdTime) {
		DataSourceContextHolder.set("dataSourceConsumeLog");
		this.consumeLogMapper.delDataApiData(thresholdTime);
	}

	public void delHourApiData(String thresholdTime) {
		DataSourceContextHolder.set("dataSourceConsumeLog");
		this.consumeLogMapper.delHourApiData(thresholdTime);
	}

}
